package com.wen.controller;

import com.wen.common.R;
import com.wen.pojo.SupplierProduct;
import com.wen.pojo.dto.SupplierProductDto;
import com.wen.service.SupplierProductService;
import com.wen.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/supplierProduct")
public class SupplierProductController {
    @Autowired
    private SupplierProductService service;

    @PostMapping("/supplierAdd")
    public R supplierAdd(@RequestBody SupplierProduct supplierProduct){
        boolean flag = service.save(supplierProduct);
            if(flag)
            return R.ok().message("添加成功");
            return  R.error().message("添加失败");
    }

    @GetMapping("/supplier/{id}")
    public R supplier(@PathVariable Long id){
      List<SupplierProductDto> supplierProducts= service.SupplierProductAll(id);
        return R.ok().data("supplier", supplierProducts);
    }

    @GetMapping("supplierById/{id}")
    public R supplierById(@PathVariable Long id){
        SupplierProduct serviceById = service.getById(id);
        return R.ok().data("serviceById", serviceById);
    }
    @PutMapping("supplierUp")
    public R supplierUp(@RequestBody SupplierProduct supplierProduct){
        boolean flag = service.updateById(supplierProduct);
        if (flag)
        return R.ok().message("修改成功");
        return R.error().message("修改失败");
    }
    @DeleteMapping("supplierDel/{id}")
    public R supplierDel(@PathVariable Long id){
        boolean flag = service.removeById(id);
        if(flag)
           return  R.ok().message("解除成功") ;
        return R.error().message("解除失败");
    }
}
